<?php

class admin_model extends Model
{
	function __construct()
	{
		parent::Model();
	}
	
	function admin_model()
	{
		self::__construct();
	}
	
	//Parse the SQL Data in _table_data() and convert into usable SQL Commands.
	function insert_tables ( $host, $username, $password, $db, $prefix ){
		
		//Connect to the DB host
		mysql_connect( $host, $username, $password ) or die( mysql_error() );
		
		//Select the DB
		mysql_select_db( $db ) or die( mysql_error() );
		
		//Collect Install Info
		$sql_array = $this->_table_data();
		
		$return = "";
		//For each command in the install info
		foreach ($sql_array as $command){
			
			//If it is a create table command
			if ( isset($command['create']) ){
				if ( $command['if_not_exists'] == 1 ){
					$query = "CREATE TABLE IF NOT EXISTS `" . $prefix . $command['create'] . "` " . base64_decode($command['params']);
					$return[] = "Creating Table (if it doesn't already exist) " . $prefix . $command['create'];
				} else {
					$query = "CREATE TABLE `" . $prefix . $command['create'] . "` " . base64_decode($command['params']);
					$return[] = "Creating Table " . $prefix . $command['create'];
				}
				mysql_query( $query );
				continue;
			}
			
			//If it is an insert into table command
			if ( isset($command['insert']) ){
				if ( $command['if_not_exists'] == 1 ){
					$query = "REPLACE INTO `" . $prefix . $command['insert'] . "` " . base64_decode($command['params']);
					$return[] = "Creating Table (if it doesn't already exist) " . $prefix . $command['insert'];
				} else {
					$query = "INSERT INTO `" . $prefix . $command['insert'] . "` " . base64_decode($command['params']);
					$return[] = "Inserting data into " . $prefix . $command['insert'];
				}
				mysql_query( $query );
				continue;
			}
			
			//If it is an alter table command
			if ( isset($command['alter']) ){
				
				$query = "ALTER IGNORE TABLE `" . $prefix . $command['alter'] . "` " . base64_decode($command['params']);
				
				@mysql_query( $query );
				$return[] = "Altering data in " . $prefix . $command['alter'];
				continue;
			}
			
			//If it is an optimize command
			if ( isset($command['optimize']) ){
				mysql_query( "OPTIMIZE TABLE `" . $prefix . $command['optimize'] . "` ");
				$return[] = "Optimizing " . $prefix . $command['optimize'];
				continue;
			}
		}
		
	}
	
	//Add an admin user to the Database.
	function add_admin($host, $dbusername, $dbpassword, $db, $prefix , $username, $password, $email, $display_name, $encryption_key){
		//Connect to the DB host
		mysql_connect( $host, $dbusername, $dbpassword ) or die( mysql_error() );
		
		//Select the DB
		mysql_select_db( $db ) or die( mysql_error() );
		
		$password = sha1($encryption_key.$password);
		
		$sql = 'INSERT INTO `'.$db.'`.`'.$prefix.'users` (`id`, `group_id`, `username`, `password`, `display_name`, `email`, `activated`, `location`) VALUES (NULL, \'1\', \''.$username.'\', \''.$password.'\', \''.$display_name.'\', \''.$email.'\', \'1\', \'\');';
		
		mysql_query($sql);
		//echo $sql;
	}
	 
	 
	//Add the settings to the Database.
	function add_settings($host, $dbusername, $password, $db, $prefix, $email, $sitename, $siteurl, $display_name, $system_email, $contact_email){
		//Connect to the DB host
		mysql_connect( $host, $dbusername, $password ) or die( mysql_error() );
		
		//Select the DB
		mysql_select_db( $db ) or die( mysql_error() );
		
		
		$sql = 'INSERT INTO `'.$prefix.'configuration` (`id`, `sitename`, `siteurl`, `administrator_name`, `administrator_email`, `system_email`, `contact_email`) VALUES
(2, \''.$sitename.'\', \''.$siteurl.'\', \''.$display_name.'\', \''.$email.'\', \''.$system_email.'\', \''.$contact_email.'\');';
		
		//echo $sql;
		
		mysql_query($sql);
	}
	
	
	//Create a random character-string.
	function random_hash($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
	{
		// Length of character list
		$chars_length = (strlen($chars) - 1);

		// Start our string
		$string = $chars{rand(0, $chars_length)};
	   
		// Generate random string
		for ($i = 1; $i < $length; $i = strlen($string))
		{
			// Grab a random character from our list
			$r = $chars{rand(0, $chars_length)};
		   
			// Make sure the same two characters don't appear next to each other
			if ($r != $string{$i - 1}) $string .=  $r;
		}
	   
		// Return the string
		return $string;
	}
	
	
	//The data used for installation of the Database.
	function _table_data (){
		// -- Create Tables
		$i = 0;
		$sql_array[$i]['create'] = "users";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgZ3JvdXBfaWRgIHRpbnlpbnQoMykgdW5zaWduZWQgTk9UIE5VTEwgZGVmYXVsdCAnMCcsCiAgYHVzZXJuYW1lYCB2YXJjaGFyKDE1KSBOT1QgTlVMTCBkZWZhdWx0ICcnLAogIGBwYXNzd29yZGAgdmFyY2hhcig2MCkgTk9UIE5VTEwsCiAgYGRpc3BsYXlfbmFtZWAgdmFyY2hhcig2MCkgTk9UIE5VTEwsCiAgYGVtYWlsYCB2YXJjaGFyKDQwKSBOT1QgTlVMTCBkZWZhdWx0ICcnLAogIGBhY3RpdmF0ZWRgIHRpbnlpbnQoMSkgTk9UIE5VTEwgZGVmYXVsdCAnMCcsCiAgYGxvY2F0aW9uYCB2YXJjaGFyKDI1NSkgTk9UIE5VTEwsCiAgUFJJTUFSWSBLRVkgIChgaWRgKQopIEVOR0lORT1NeUlTQU0gIERFRkFVTFQgQ0hBUlNFVD11dGY4IEFVVE9fSU5DUkVNRU5UPTc3IDs=";

		$i++;

		$sql_array[$i]['optimize'] = "users";
		
		$i++;
		
		$sql_array[$i]['create'] = "configuration";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgc2l0ZW5hbWVgIHZhcmNoYXIoNjApIE5PVCBOVUxMLAogIGBzaXRldXJsYCB2YXJjaGFyKDYwKSBOT1QgTlVMTCwKICBgYWRtaW5pc3RyYXRvcl9uYW1lYCB2YXJjaGFyKDYwKSBOT1QgTlVMTCwKICBgYWRtaW5pc3RyYXRvcl9lbWFpbGAgdmFyY2hhcig2MCkgTk9UIE5VTEwsCiAgYHN5c3RlbV9lbWFpbGAgdmFyY2hhcig2MCkgTk9UIE5VTEwsCiAgYGNvbnRhY3RfZW1haWxgIHZhcmNoYXIoMjU1KSBOT1QgTlVMTCwKICBQUklNQVJZIEtFWSAgKGBpZGApCikgRU5HSU5FPU15SVNBTSAgREVGQVVMVCBDSEFSU0VUPWxhdGluMSBBVVRPX0lOQ1JFTUVOVD0zIDs=";

		$i++;

		$sql_array[$i]['optimize'] = "configuration";
		
		$i++;
		
		$sql_array[$i]['create'] = "contact";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgbmFtZWAgdmFyY2hhcigyNTUpIE5PVCBOVUxMLAogIGBlbWFpbGAgdmFyY2hhcigyNTUpIE5PVCBOVUxMLAogIGBzdWJqZWN0YCB2YXJjaGFyKDI1NSkgTk9UIE5VTEwsCiAgYG1lc3NhZ2VgIHRleHQgTk9UIE5VTEwsCiAgUFJJTUFSWSBLRVkgIChgaWRgKQopIEVOR0lORT1NeUlTQU0gIERFRkFVTFQgQ0hBUlNFVD1sYXRpbjEgQVVUT19JTkNSRU1FTlQ9OCA7Cg==";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "contact";
		
		$i++;
		
		$sql_array[$i]['create'] = "groups";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCwKICBgdGl0bGVgIHZhcmNoYXIoMjApIE5PVCBOVUxMIGRlZmF1bHQgJycsCiAgYGRlc2NyaXB0aW9uYCB2YXJjaGFyKDEwMCkgTk9UIE5VTEwgZGVmYXVsdCAnJywKICBQUklNQVJZIEtFWSAgKGBpZGApCikgRU5HSU5FPU15SVNBTSBERUZBVUxUIENIQVJTRVQ9dXRmODs=";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "groups";
		
		$i++;
		
		$sql_array[$i]['create'] = "header_injections";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgbmFtZWAgdGV4dCBOT1QgTlVMTCwKICBgdHlwZWAgdGV4dCBOT1QgTlVMTCwKICBgaW5qZWN0aW9uYCB0ZXh0IE5PVCBOVUxMLAogIGBhZG1pbmAgdGlueWludCgxKSBOT1QgTlVMTCBkZWZhdWx0ICcwJyBDT01NRU5UICdTaG93IG9uIHRoZSBBZG1pbiBQYW5lbD8nLAogIGBtYWluYCB0aW55aW50KDEpIE5PVCBOVUxMIGRlZmF1bHQgJzEnIENPTU1FTlQgJ1Nob3cgb24gdGhlIG1haW4gcGFnZT8nLAogIFBSSU1BUlkgS0VZICAoYGlkYCkKKSBFTkdJTkU9TXlJU0FNICBERUZBVUxUIENIQVJTRVQ9bGF0aW4xIEFVVE9fSU5DUkVNRU5UPTE0IDs=";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "header_injections";
		
		
		$i++;
		
		$sql_array[$i]['create'] = "module_list";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgbmFtZWAgdGV4dCBOT1QgTlVMTCwKICBgZGVzY3JpcHRpb25gIHRleHQgTk9UIE5VTEwsCiAgYHByb2plY3R1cmxgIHRleHQgTk9UIE5VTEwsCiAgUFJJTUFSWSBLRVkgIChgaWRgKQopIEVOR0lORT1NeUlTQU0gIERFRkFVTFQgQ0hBUlNFVD1sYXRpbjEgQVVUT19JTkNSRU1FTlQ9MzAgOw==";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "module_list";
		
		
		$i++;
		
		$sql_array[$i]['create'] = "pages";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgbGlua2AgdmFyY2hhcigyNTUpIE5PVCBOVUxMLAogIGB0aXRsZWAgdmFyY2hhcigyNTUpIE5PVCBOVUxMLAogIGBjb250ZW50YCB0ZXh0IE5PVCBOVUxMLAogIGByb3V0ZV9pZGAgaW50KDExKSBOT1QgTlVMTCwKICBQUklNQVJZIEtFWSAgKGBpZGApCikgRU5HSU5FPU15SVNBTSAgREVGQVVMVCBDSEFSU0VUPWxhdGluMSBBVVRPX0lOQ1JFTUVOVD04NCA7";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "pages";
		
		$i++;
		
		$sql_array[$i]['create'] = "recent";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgdXNlcm5hbWVgIHZhcmNoYXIoMjU1KSBOT1QgTlVMTCwKICBgbW9kdWxlYCB2YXJjaGFyKDI1NSkgTk9UIE5VTEwsCiAgYGFjdGlvbmAgdmFyY2hhcigyNTUpIE5PVCBOVUxMLAogIFBSSU1BUlkgS0VZICAoYGlkYCkKKSBFTkdJTkU9TXlJU0FNICBERUZBVUxUIENIQVJTRVQ9bGF0aW4xIEFVVE9fSU5DUkVNRU5UPTU0MiA7Cg==";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "recent";
		
		$i++;
		
		$sql_array[$i]['create'] = "routes";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgZmFrZV9yb3V0ZWAgdmFyY2hhcigyNTUpIE5PVCBOVUxMLAogIGByZWFsX3JvdXRlYCB2YXJjaGFyKDI1NSkgTk9UIE5VTEwsCiAgYHRpdGxlYCB2YXJjaGFyKDYwKSBOT1QgTlVMTCwKICBgZnJvbnRfbmF2YCBpbnQoNTApIE5PVCBOVUxMIGRlZmF1bHQgJzEnLAogIGBuYXZpZ2F0aW9uX29yZGVyYCBpbnQoMTEpIE5PVCBOVUxMIGRlZmF1bHQgJzAnLAogIGBwYXJlbnRfaWRgIGludCgxMSkgTk9UIE5VTEwsCiAgUFJJTUFSWSBLRVkgIChgaWRgKQopIEVOR0lORT1NeUlTQU0gIERFRkFVTFQgQ0hBUlNFVD1sYXRpbjEgQVVUT19JTkNSRU1FTlQ9NjUgOw==";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "routes";
		
		
		$i++;
		
		$sql_array[$i]['create'] = "themes";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KAogIGBpZGAgaW50KDExKSBOT1QgTlVMTCBhdXRvX2luY3JlbWVudCwKICBgbmFtZWAgdmFyY2hhcig2MCkgTk9UIE5VTEwsCiAgYGFjdGl2ZWAgaW50KDExKSBOT1QgTlVMTCwKICBQUklNQVJZIEtFWSAgKGBpZGApCikgRU5HSU5FPU15SVNBTSAgREVGQVVMVCBDSEFSU0VUPWxhdGluMSBBVVRPX0lOQ1JFTUVOVD03IDs=";
		
		$i++;
		
		$sql_array[$i]['optimize'] = "themes";
		
		
		// -- Insert Defaults
		
		$i++;
		
		$sql_array[$i]['insert'] = "themes";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] =  "KGBpZGAsIGBuYW1lYCwgYGFjdGl2ZWApIFZBTFVFUw0KKDEsICdzeW50YXhtb25zdGVyJywgMSk7";
		
		$i++;

		$sql_array[$i]['insert'] = "routes";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KGBpZGAsIGBmYWtlX3JvdXRlYCwgYHJlYWxfcm91dGVgLCBgdGl0bGVgLCBgZnJvbnRfbmF2YCwgYG5hdmlnYXRpb25fb3JkZXJgLCBgcGFyZW50X2lkYCkgVkFMVUVTCigxLCAnaG9tZScsICdwYWdlcy9pbmRleC9ob21lJywgJ0hvbWUnLCAxLCAwLCAwKSwKKDIsICdkZWZhdWx0X2NvbnRyb2xsZXInLCAncGFnZXMnLCAnJywgMCwgMCwgMCk7Cgo=";
		
		$i++;

		$sql_array[$i]['insert'] = "recent";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KGBpZGAsIGB1c2VybmFtZWAsIGBtb2R1bGVgLCBgYWN0aW9uYCkgVkFMVUVTDQooTlVMTCwgJ04vQScsICdjb3JlJywgJ0luc3RhbGxlZCBTeW50YXhDTVMnKTs=";
		
		$i++;
		
		$sql_array[$i]['insert'] = "pages";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KGBpZGAsIGBsaW5rYCwgYHRpdGxlYCwgYGNvbnRlbnRgLCBgcm91dGVfaWRgKSBWQUxVRVMKKDI1LCAnaG9tZScsICdIb21lJywgJzxoMT5XZWxjb21lITwvaDE+PGgyPlRoYW5rIHlvdSBmb3IgaW5zdGFsbGluZyBTeW50YXhDTVMuPC9oMj48cD5UaGlzIGlzIGV4YW1wbGUgY29udGVudCBvbiBhbiBleGFtcGxlIHRoZW1lLiBZb3UgY2FuIG1vZGlmeSB0aGlzIGJ5IGdvaW5nIHRvIHlvdXIgPGEgaHJlZj0iaW5kZXgucGhwL2FkbWluaXN0cmF0aW9uIj5BZG1pbiBQYWdlPC9hPi48L3A+JywgMSk7";
		
		$i++;

		$sql_array[$i]['insert'] = "groups";
		$sql_array[$i]['if_not_exists'] = 1;
		$sql_array[$i]['params'] = "KGBpZGAsIGB0aXRsZWAsIGBkZXNjcmlwdGlvbmApIFZBTFVFUw0KKDEsICdBZG1pbicsICdTaXRlIEFkbWluaXN0cmF0b3JzJyksDQooMCwgJ0FueW9uZScsICdBbnlvbmUnKTs=";
		
		return $sql_array;
	}
}


?>
